package handler

import (
	"0519/api/client"
	"0519/api/model"
	"0519/common/pkg"
	course "0519/common/proto"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserLogin(c *gin.Context) {
	var data model.UserLogin
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	add, err := client.UserLogin(c, &course.UserLoginReq{
		Mobile:   data.Mobile,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	claims := pkg.CustomClaims{
		ID: uint(add.UserId),
	}
	token, err := pkg.NewJWT("2209A").CreateToken(claims)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "Token生成错误",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户登录成功",
		"date": token,
	})
}

func CourseAdd(c *gin.Context) {
	var data model.CourseAdd
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CourseAdd(c, &course.CourseAddReq{
		Name:              data.Name,
		CourseInformation: data.CourseInformation,
		CourseDuration:    data.CourseDuration,
		Price:             float32(data.Price),
		Stock:             int64(data.Stock),
		State:             int64(data.State),
		Picture:           data.Picture,
		UserId:            int64(userId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程发布成功",
		"date": add,
	})
}

func CourseList(c *gin.Context) {
	userId := c.GetUint("userId")
	add, err := client.CourseList(c, &course.CourseListReq{UserId: int64(userId)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程列表展示成功",
		"date": add,
	})
}

func CourseInfo(c *gin.Context) {
	var data model.CourseInfo
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CourseInfo(c, &course.CourseInfoReq{
		CourseId: int64(data.CourseId),
		UserId:   int64(userId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程详情展示成功",
		"date": add,
	})
}

func CourseUpdateState(c *gin.Context) {
	var data model.CourseUpdateState
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CourseUpdateState(c, &course.CourseUpdateStateReq{
		UserId:   int64(userId),
		CourseId: int64(data.CourseId),
		State:    int64(data.State),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程状态修改成功",
		"date": add,
	})
}

func CourseDeleted(c *gin.Context) {
	var data model.CourseDeleted
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CourseDeleted(c, &course.CourseDeletedReq{
		UserId:   int64(userId),
		CourseId: int64(data.CourseId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程删除成功",
		"date": add,
	})
}

func CreateDiscountCourse(c *gin.Context) {
	var data model.CreateDiscountCourse
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CreateDiscountCourse(c, &course.CreateDiscountCourseReq{
		UserId:    int64(userId),
		CourseId:  data.CourseId,
		StartTime: data.StartTime,
		EndTime:   data.EndTime,
		NowPrice:  float32(data.NowPrice),
		Num:       data.Num,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "限时优惠商品发布成功",
		"date": add,
	})
}

func CreateDiscountCourseOrder(c *gin.Context) {
	var data model.CreateDiscountCourseOrder
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	add, err := client.CreateDiscountCourseOrder(c, &course.CreateDiscountCourseOrderReq{
		UserId:   int64(userId),
		CourseId: data.CourseId,
		Num:      data.Num,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建课程订单成功",
		"date": add,
	})
}

func OrderList(c *gin.Context) {
	userId := c.GetUint("userId")
	add, err := client.OrderList(c, &course.OrderListReq{UserId: int64(userId)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单统计成功",
		"date": add,
	})
}

func OrderPay(c *gin.Context) {
	var data model.OrderPay
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	add, err := client.OrderPay(c, &course.OrderPayReq{
		OrderId: data.OrderId,
		Pay:     data.Pay,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单支付成功",
		"date": add,
	})
}
